package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class ItemNamePane extends AdjustablePanel {
    private JLabel lblItemName;
    private JTextField tfItemName;

    public ItemNamePane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblItemName)).addGroup(layout.createParallelGroup().addComponent(this.tfItemName));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblItemName).addComponent(this.tfItemName));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(1, this.lblItemName, this.tfItemName);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntText = UIManager.getDefaults().getFont("TextField.font");
        if (fntText == null) {
            fntText = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntText = fntText.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_ITEM_NAME"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.lblItemName == null) {
            this.lblItemName = new JLabel();
        }
        this.lblItemName.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_ITEM_NAME"));
        this.lblItemName.setFont(fntLabel);
        if (this.tfItemName == null) {
            this.tfItemName = new JTextField();
        }
        this.tfItemName.setFont(fntText);
    }

    public void clear() {
        this.tfItemName.setText("");
    }

    public void addCriteria(final SelectionCriteria criteria) {
        String s = this.tfItemName.getText();
        if (s != null && !s.isEmpty()) {
            for (int pos = s.indexOf("*"); pos != -1; pos = s.indexOf("*")) {
                s = s.substring(0, pos) + "%" + s.substring(pos + 1);
            }
        }
        if (!s.startsWith("%")) {
            s = "%" + s;
        }
        if (!s.endsWith("%")) {
            s += "%";
        }
        criteria.itemName = s;
    }

    public void addCriteriaText(final SelectionCriteria criteria) {
        String s = this.tfItemName.getText();
        criteria.itemName = s;
    }
}
